package com.tiyende.kash.loop.ui.adapter

import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonRecyclerViewHolder
import com.lib.common.adapter.CommonViewHolderHelper
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.utils.TiyendeImageUtil
import com.tiyende.kash.loop.utils.helper.TiyendeCustomDataHelper
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/27 13:45
 * @Annotation：在线客服会话
 */
class TiyendeCustomerChatAdapter(recyclerView: RecyclerView?): CommonRecyclerViewAdapter<JSONObject>(recyclerView) {

    //快捷发送问题点击回调
    private var guessItemListener: IChatGuessClickListener? = null
    fun setChatGuessItemClickListener(listener: IChatGuessClickListener){
        guessItemListener = listener
    }

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        val itemType = model.optString("itemType")
        return when(itemType){
            "itemSend" -> {
                R.layout.tiyende_activity_customer_chat_send_item
            }

            "itemReceived" -> {
                R.layout.tiyende_activity_customer_chat_received_item
            }

            "itemGuess" -> {
                R.layout.tiyende_activity_customer_chat_guess_item
            }
            //"itemWait"
            else -> {
                R.layout.tiyende_activity_customer_chat_send_item
//                R.layout.act_gombe_credit_user_chat_wait_item
            }
        }
    }
    override fun setItemChildListener(helper: CommonViewHolderHelper, viewType: Int) {
    }

    override fun onBindViewHolder(holder: CommonRecyclerViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads.isEmpty()){
            onBindViewHolder(holder,position)
            return
        }
        val helper = holder.viewHolderHelper
        val model = getItem(position) as JSONObject
        val itemType = model.optString("itemType")
        if (itemType == "itemSend"){
            showChatSendView(helper,position,model)
        }
    }

    override fun onBindViewHolder(viewHolder: CommonRecyclerViewHolder, position: Int) {
        super.onBindViewHolder(viewHolder, position)
        val helper = viewHolder.viewHolderHelper
        val model = getItem(position) as JSONObject
        when(val itemType = model.optString("itemType")){
            "itemSend",
            "itemReceived" -> {
                helper.setText(R.id.textChatMsg,model.optString("talkInfo"))

                if (itemType == "itemSend"){
                    showChatSendView(helper,position,model)
                }
            }
            "itemGuess" -> {
                showChatGuessView(helper,position,model)
            }
        }
    }

    override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
//        when(val itemType = model.optString("itemType")){
//            "itemSend",
//            "itemReceived" -> {
//                helper.setText(R.id.textChatMsg,model.optString("talkInfo"))
//
//                if (itemType == "itemSend"){
//                    showChatSendView(helper,position,model)
//                }
//            }
//        }
    }

    //发送
    private fun showChatSendView(helper: CommonViewHolderHelper, position: Int, model: JSONObject){
        val customerChatSendHeader = helper.getImageView(R.id.customerChatSendHeader)
        val avatarUrl = TiyendeCustomDataHelper.queryTiyendeCustomUserAvatarUrl()
        if (avatarUrl.isNotEmpty()){
            TiyendeImageUtil.instance?.loadImageUrl(mContext,customerChatSendHeader,avatarUrl)
        } else {
            customerChatSendHeader.setImageResource(R.mipmap.tiyende_chat_send_header_icon)
        }

        helper.setVisibility(R.id.readPointLayout, View.GONE)
        helper.setVisibility(R.id.customerChatWaitLayout, View.GONE)
        //最后一条消息 && 发送
        if (position == itemCount - 1){
            helper.setVisibility(R.id.readPointLayout, View.VISIBLE)
            val readStatus1 = helper.getImageView(R.id.customerChatSendReadStatus1)
            val readStatus2 = helper.getImageView(R.id.customerChatSendReadStatus2)

            val createTimeStamp = model.optString("createTimeStamp","0")
            val intervalTime = (System.currentTimeMillis() - createTimeStamp.toLong()) / 1000
            if (intervalTime >= 10){
                readStatus1.setImageResource(R.mipmap.tiyende_chat_send_read_selected_icon)
                readStatus2.setImageResource(R.mipmap.tiyende_chat_send_read_selected_icon)
                helper.setVisibility(R.id.customerChatWaitLayout, View.VISIBLE)
            } else if (intervalTime in 6..10){
                readStatus1.setImageResource(R.mipmap.tiyende_chat_send_read_selected_icon)
                readStatus2.setImageResource(R.mipmap.tiyende_chat_send_read_normal_icon)
            } else {
                readStatus1.setImageResource(R.mipmap.tiyende_chat_send_read_normal_icon)
                readStatus2.setImageResource(R.mipmap.tiyende_chat_send_read_normal_icon)
            }
        }
    }

    /**
     * Guess
     * 快捷发送
     */
    private fun showChatGuessView(helper: CommonViewHolderHelper, position: Int, model: JSONObject){
        val question1Layout = helper.getView<LinearLayout>(R.id.guessItem1)
        question1Layout.setOnClickListener(IQuestionGuessClickListener)

        val question2Layout = helper.getView<LinearLayout>(R.id.guessItem2)
        question2Layout.setOnClickListener(IQuestionGuessClickListener)

        val question3Layout = helper.getView<LinearLayout>(R.id.guessItem3)
        question3Layout.setOnClickListener(IQuestionGuessClickListener)

        val question4Layout = helper.getView<LinearLayout>(R.id.guessItem4)
        question4Layout.setOnClickListener(IQuestionGuessClickListener)

        val question5Layout = helper.getView<LinearLayout>(R.id.guessItem5)
        question5Layout.setOnClickListener(IQuestionGuessClickListener)

        val question6Layout = helper.getView<LinearLayout>(R.id.guessItem6)
        question6Layout.setOnClickListener(IQuestionGuessClickListener)
    }

    private val IQuestionGuessClickListener = View.OnClickListener { v ->
        when(v?.id){
            R.id.guessItem1 -> {
                guessItemListener?.onGuessItemClick(mContext.getString(R.string.tiyende_customer_chat_guess_item1))
            }

            R.id.guessItem2 -> {
                guessItemListener?.onGuessItemClick(mContext.getString(R.string.tiyende_customer_chat_guess_item2))
            }

            R.id.guessItem3 -> {
                guessItemListener?.onGuessItemClick(mContext.getString(R.string.tiyende_customer_chat_guess_item3))
            }

            R.id.guessItem4 -> {
                guessItemListener?.onGuessItemClick(mContext.getString(R.string.tiyende_customer_chat_guess_item4))
            }

            R.id.guessItem5 -> {
                guessItemListener?.onGuessItemClick(mContext.getString(R.string.tiyende_customer_chat_guess_item5))
            }

            R.id.guessItem6 -> {
                guessItemListener?.onGuessItemClick(mContext.getString(R.string.tiyende_customer_chat_guess_item6))
            }
        }
    }

    interface IChatGuessClickListener{
        fun onGuessItemClick(text: String)
    }
}